package com.wc.alorithm_luogu.P2024;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

/**
 * @Author congege
 * 食物链
 * https://www.luogu.com.cn/problem/P2024
 * @Date Created in 2023/11/1-15:41
 */
public class Main {
    static int[] circle;

    public static void main(String[] args) {
        Input sc = new Input();
        int n = sc.nextInt();
        int m = sc.nextInt();
        circle = new int[3 * n + 1];
        for (int i = 1; i <= 3 * n; i++) {
            circle[i] = i;
        }
        int ans = 0;
        Main main = new Main();
        while (m-- > 0) {
            int type = sc.nextInt();
            int idx = sc.nextInt();
            int idy = sc.nextInt();
            if (idx > n || idy > n) {
                ans++;
                continue;
            }
            if (type == 2) {
                // 不能是同类或者是反吃的关系
                if (main.find(idy + n) == main.find(idx) || main.find(idx) == main.find(idy)) {
                    ans++;
                    continue;
                }
                main.merge(idx, idy + 2 * n);
                main.merge(idx + n, idy);
                main.merge(idx + 2 * n, idy + n);
            } else {
                // 不能是吃和被吃的关系
                if (main.find(idx + n) == main.find(idy) || main.find(idy + n) == main.find(idx)) {
                    ans++;
                    continue;
                }
                main.merge(idx, idy);
                main.merge(idx + n, idy + n);
                main.merge(idx + 2 * n, idy + 2 * n);
            }
        }
        System.out.println(ans);
    }

    public void merge(int idx, int idy) {
        int preX = find(idx);
        int preY = find(idy);
        if (preX != preY) {
            circle[preX] = preY;
        }
    }

    public int find(int x) {
        if (circle[x] == x) {
            return x;
        }
        return find(circle[x]);
    }


    static class Input {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public int nextInt() {
            try {
                in.nextToken();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return (int) in.nval;
        }
    }
}
